package com.woniu113.junk.order.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.commons.util.TokenUtil;
import com.woniu113.junk.order.dto.AllGoodsOrderDTO;
import com.woniu113.junk.order.dto.GoodsOrderDTO;
import com.woniu113.junk.order.dto.GoodsOrderInfoDTO;
import com.woniu113.junk.order.form.AddGoodsOrderForm;
import com.woniu113.junk.order.form.FindGoodsByAdminForm;
import com.woniu113.junk.order.form.FindGoodsForm;
import com.woniu113.junk.order.form.FindGoodsOrderForm;
import com.woniu113.junk.order.model.GoodsOrderInfo;
import com.woniu113.junk.order.param.AddGoodsOrderParam;
import com.woniu113.junk.order.param.FindGoodsByAdminParam;
import com.woniu113.junk.order.param.FindGoodsOrderParam;
import com.woniu113.junk.order.param.FindGoodsParam;
import com.woniu113.junk.order.service.GoodsOrderInfoService;
import com.woniu113.junk.order.util.Data;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/goodsOrderInfo")
@Api(tags = "积分商品订单相关接口")
public class GoodsOrderInfoController {

    @Resource
    private GoodsOrderInfoService gois;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 创建积分商品订单
     * @param form 下单用户ID，地址ID，积分商品ID以及商品数量
     * @return 无数据返回
     * @author 小周
     * @date 2024/10/17
     */
    @PostMapping("/buy")
    @ApiOperation(value = "创建积分商品订单", notes = "下单用户ID，地址ID，积分商品ID以及商品数量")
    public Result<GoodsOrderInfo> buy(@RequestBody AddGoodsOrderForm form,@RequestHeader String token) throws Exception {
        System.out.println(form.getGoodsId());
        System.out.println(form.getNumber());
        System.out.println(form.getAddressId());
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        AddGoodsOrderParam param = BeanUtil.toBean(form, AddGoodsOrderParam.class);
        param.setUserId(userId);
        gois.buy(param);
        return Result.getResult(GoodsOrderInfo.class).setCode(200).setMsg("新增积分商品订单成功");
    }

    /**
     * 查询特定用户的积分商品订单信息
     * @param form 查询条件参数：用户ID, 积分商品类型
     * @return 包含指定用户的积分商品订单信息的分页结果
     * @author 小周
     * @date 2024/10/17
     */
    @PostMapping("/findByUserId")
    @ApiOperation(value = "查询特定用户的积分商品订单信息", notes = "根据用户ID和积分商品类型查询订单信息")
    public Result<Page<GoodsOrderInfo>> findByUserId(@RequestBody FindGoodsOrderForm form,@RequestHeader String token) throws Exception {
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        FindGoodsOrderParam param = BeanUtil.toBean(form, FindGoodsOrderParam.class);
        param.setUserId(userId);
        Page<GoodsOrderInfo> page = gois.findByUserId(param);
        return Result.getPageResult(GoodsOrderInfo.class).setCode(200).setMsg("查询特定用户积分商品订单成功").setData(page);
    }

    /**
     * 删除指定用户的积分商品订单
     * @param orderId 订单ID
     * @return 删除成功的响应结果
     * @author 小周
     * @date 2024/10/17
     */
    @GetMapping("/delete/{orderId}")
    @ApiOperation(value = "删除指定用户的积分商品订单", notes = "根据订单ID删除订单")
    @ApiImplicitParam(name = "orderId", value = "订单ID", required = true, dataType = "Integer", paramType = "path")
    public Result<GoodsOrderInfo> delete(@PathVariable Integer orderId) {
        GoodsOrderInfo orderInfo = gois.getById(orderId);
        orderInfo.setState(Data.GOODS_STATE_DELETE);
        gois.updateById(orderInfo);
        return Result.getResult(GoodsOrderInfo.class).setCode(200).setMsg("删除积分商品订单成功");
    }

    /**
     * 修改指定订单状态为待收货
     * @param orderId 订单ID
     * @return 修改成功的响应结果
     * @author 小周
     * @date 2024/10/17
     */
    @GetMapping("/deliver")
    @ApiOperation(value = "修改指定订单状态为待收货", notes = "根据订单ID将订单状态修改为待收货")
    @ApiImplicitParam(name = "orderId", value = "订单ID", required = true, dataType = "Integer", paramType = "path")
    public Result<GoodsOrderInfo> deliver(@RequestParam("orderId") Integer orderId) {
        GoodsOrderInfo orderInfo = gois.getById(orderId);
        orderInfo.setOrderState(Data.GOODS_ORDER_STATE_DELIVER);
        gois.updateById(orderInfo);
        return Result.getResult(GoodsOrderInfo.class).setCode(200).setMsg("积分商品订单状态修改为待收货成功");
    }

    /**
     * 修改指定订单状态为已收货
     * @param orderId 订单ID
     * @return 修改成功的响应结果
     * @author 小周
     * @date 2024/10/17
     */
    @GetMapping("/over/{orderId}")
    @ApiOperation(value = "修改指定订单状态为已收货", notes = "根据订单ID将订单状态修改为已收货")
    @ApiImplicitParam(name = "orderId", value = "订单ID", required = true, dataType = "Integer", paramType = "path")
    public Result<GoodsOrderInfo> over(@PathVariable Integer orderId) {
        GoodsOrderInfo orderInfo = gois.getById(orderId);
        orderInfo.setOrderState(Data.GOODS_ORDER_STATE_OVER);
        gois.updateById(orderInfo);
        return Result.getResult(GoodsOrderInfo.class).setCode(200).setMsg("积分商品订单状态修改为已收货成功");
    }

    /**
     * 查询当前用户订单
     * */
    @PostMapping("/findByUser")
    public Result<List<GoodsOrderInfo>> findByUser(@RequestBody FindGoodsForm form,@RequestHeader String token) throws Exception {
        int userId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        FindGoodsParam param = BeanUtil.toBean(form, FindGoodsParam.class);
        param.setUserId(userId);
        GoodsOrderDTO dto = gois.findByUser(param);
        return Result.getListResult(GoodsOrderInfo.class).setCode(200).setMsg("查询积分商品订单信息成功").setTotal(dto.getTotal()).setData(dto.getList());
    }

    /**
     * 查询积分订单(管理端使用)
     * */
    @PostMapping("/findByAdmin")
    public Result<List<GoodsOrderInfoDTO>> findByAdmin(@RequestBody FindGoodsByAdminForm form) throws Exception {
        FindGoodsByAdminParam param = BeanUtil.toBean(form, FindGoodsByAdminParam.class);
        AllGoodsOrderDTO dto = gois.findByAdmin(param);
        return Result.getListResult(GoodsOrderInfoDTO.class).setCode(200).setMsg("查询积分商品订单信息成功").setTotal(dto.getTotal()).setData(dto.getList());
    }
}


